package com.kefu.client.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.kefu.client.services.ClientServices;
import com.kefu.model.ClientUser;
import com.kefu.util.Time;

@Controller
@Scope("prototype")
public class ClientUserRegister {

	public ClientUserRegister() {
		System.out.println("Info:init client user register controller...   Time:"+Time.getNowTime());
	}

	@Autowired
	ClientServices clientServices;
	
	@RequestMapping("/register.c")
	public void register(HttpServletResponse response) throws IOException{
		//生成客户端编号
		String user_uuid=java.util.UUID.randomUUID().toString();
		//生成用户对象
		ClientUser user=ClientUser.createUser(user_uuid,Time.getNowTime());
		//将创建的用户记录到数据库
		user=clientServices.saveUserInfo(user);
		//生成用户信息json数据
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("user",user);
		String result_json=JSONObject.toJSONString(map);
		//设置响应
		response.setCharacterEncoding("UTF-8");    
		response.setContentType("application/json; charset=utf-8");  
		//返回用户对象
		PrintWriter pw=response.getWriter();
		pw.write(result_json);
		pw.close();
	}
	
}
